// pages/member/memberBank/memberBank.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    bank_name: "",
    bank_id: "",
    bank_user: "",
    bank_province: "",
    bank_city: "",
    bank_branch: "",
    bankIdcard:"",
    bankMobile:"",
    idcardShow:false,
    mobileShow:false,
    status:false,
    result:'1'
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this._getData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  //绑定开户行的值
  banknameinp(e) {
    let val = e.detail;
    let newVal = val.replace(/[0-9]+/g,'')
    this.setData({
      "bank_name": newVal.trim()

    })

  
  },
  onChangRadio(e){
    this.setData({
      result:e.detail
    })
  },
  //绑定开户行省份的值
  bankprovinceinp(e) {
    let val = e.detail.value;
    this.setData({
      "bank_province": val.trim()
    });
  },
  //绑定开户行城市的值
  bankcityinp(e) {
    let val = e.detail.value;
    this.setData({
      "bank_city": val.trim()
    });
  },
  //绑定开户行支行的值
  bankbranchinp(e) {
    let val = e.detail.value;
    this.setData({
      "bank_branch": val.trim()
    });
  },
  //绑定账号的值
  bankidinp(e) {
    let val = e.detail.value;
    this.setData({
      "bank_id": val.trim()
    });
  },
  //绑定姓名的值
  bankuserinp(e) {
    let val = e.detail.value;
    this.setData({
      "bank_user": val.trim()
    });
  },
  bankChange(e) {
    let val = e.detail;
    let {name} = e.target.dataset;
    this.setData({
      [name]:val.trim()
    })
  },
  //获取信息
  _getData() {
    // let json = {};
    let that = this;
    let urlStr = app.getNetAddresss("member.bank-card.show");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: this.data.form,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          if(data.hasOwnProperty('mobile')) { //查询对象有没有这个属性名，后端返回字段就是开关开启，没有返回就是关闭
            that.data.bankMobile = data.mobile;
            that.data.mobileShow = true;
          }
          if(data.hasOwnProperty('idcard')) { 
            that.data.bankIdcard = data.idcard;
            that.data.idcardShow = true;
          }
          that.setData({
            bank_name: res.data.bank_name,
            bank_id: res.data.bank_card,
            bank_user: res.data.member_name,
            bank_province: res.data.bank_province,
            bank_city: res.data.bank_city,
            bank_branch: res.data.bank_branch,
            bankMobile:data.mobile,
            bankIdcard:data.idcard,
            mobileShow:that.data.mobileShow,
            idcardShow:that.data.idcardShow
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  change(){
    this.setData({
      // this.status = true;
      // this.bank_id = "";
      status:true,
      bank_id:''
    });
  },
  //注册
  bind() {
    // let that = this;
    if (app._isTextEmpty(this.data.bank_name)) {
      wx.showToast({
        icon: 'none',
        title: '请输入开户行',
        duration: 1500
      });
      return;
    }

    if (app._isTextEmpty(this.data.bank_province)) {
      wx.showToast({
        icon: 'none',
        title: '请输入开户行省份',
        duration: 1500
      });
      return;
    }

    if (app._isTextEmpty(this.data.bank_city)) {
      wx.showToast({
        icon: 'none',
        title: '请输入开户行城市',
        duration: 1500
      });
      return;
    }


    if (app._isTextEmpty(this.data.bank_branch)) {
      wx.showToast({
        icon: 'none',
        title: '请输入开户行支行',
        duration: 1500
      });
      return;
    }

    if (app._isTextEmpty(this.data.bank_id)) {
      wx.showToast({
        icon: 'none',
        title: '请输入账号',
        duration: 1500
      });
      return;
    }

    if (app._isTextEmpty(this.data.bank_user)) {
      wx.showToast({
        icon: 'none',
        title: '请输入姓名',
        duration: 1500
      });
      return;
    }
    if (app._isTextEmpty(this.data.bankIdcard) && this.data.idcardShow) {
      wx.showToast({
        icon: 'none',
        title: '请输入身份证',
        duration: 1500
      });
      return;
    }
    if (app._isTextEmpty(this.data.bankMobile) && this.data.mobileShow) {
      app.tips('请输入银行卡预留手机号')
      return;
    }
    let json = {
      'bank_card': this.data.bank_id,
      'member_name': this.data.bank_user,
      'bank_name': this.data.bank_name,
      "bank_province": this.data.bank_province,
      "bank_city": this.data.bank_city,
      "bank_branch": this.data.bank_branch,
      "bank_type":this.data.result,
    };
    if(this.data.idcardShow) {
      json.idcard = this.data.bankIdcard;
    }
    if(this.data.mobileShow) {
      json.mobile = this.data.bankMobile;
    }
    let urlStr = app.getNetAddresss("member.bank-card.edit");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
});
